$(function(){
    loadFoot(1);
    share();
    var did=$.getUrlParam('did')||'';//知识库四个大按钮跳转时记住的id
    $.ajax({
        url:host+'/CMSAPI/MainInList?index=1&size=4&categorys=146&menuID=7&cid='+cid+"&recommendType=5",
        type:'GET',
        dataType:'JSON',
        success:function(data){
            /*******第一部分的banner部分*********/
            if(data.state){
                var list1=data.data.list;
                var html1='';
                $.each(list1,function(i,obj){
                    var imgUrl=obj.images.length>=1?obj.images[0].URL:'';
                    var dataId=obj.id;//每条详情的id
                    var url=obj.url||'detail.html?did='+dataId;//没有填写url就填写默认url
                    html1+='<div class="swiper-slide">';
                    html1+=   '<a href="'+url+'"  data-id="'+dataId+'">';
                    html1+=      '<img class="img-response" src="'+imgUrl+'" alt="">';
                    html1+=   '</a>';
                    html1+='</div>'
                });
                $('.rBannerWrapper').append(html1);
                var rSwiper=new Swiper('.rBanner',{
                    autoplay:2000,
                    speed:800,
                    loop:true,
                    autoplayDisableOnInteraction:false,
                    pagination : '.swiper-pagination'
                });
                //TODO:设置banner部分的宽高比例是2：1
                var winWidth=$('body').width();
                $('.swiper-slide').height(.5*winWidth)
            }else {
                if(isBuggering){
                    alert(data.message)
                }
            }
        }
    });
    ///获取tab集合
    $.ajax({
        url:host+'/CMSAPI/CategoryByLevel?cid='+cid+'&id=146&menuID=7&level=2',
        type:'GET',
        dataType:'JSON',
        success:function(data){
            if(data.state){
                var tabs=data.data;
                var tabLen=tabs.length;
                var tabHtml='';
                var ddid='';//知识库四个大按钮跳转时进去时记住第一个tab的id
                $.each(tabs,function(i,tab){
                    var tabName=tab.GroupName||'';
                    var tabId=tab.GroupID||'';
                    var acClass='';
                    if(i==0){acClass='tabActive';ddid=tab.GroupID}
                    tabHtml+='<li class="tabList '+acClass+'" data-id="'+tabId+'" style="width:'+(100/tabLen)+'%">'+$.limitWords({content:tabName,number:5})+'</li>'
                });
                $('.tabBox').append(tabHtml);
                addList(ddid);
            }else {
                alert('网络繁忙，请稍后重试')
            }
        }
    });
    /********知识列表*******/
    function addList(ddid){
        // if($('.dropload-down').length!==0){
        //     $('.dropload-down').remove();
        // }
        var index=0;
        var size=50;
        var message=0;
        var fid=ddid||did;//第一次进来时间初始化的第一列表id  first id
        $('section').dropload({
            scrollArea:window,
            distance:50,
            domUp:{
                domClass : 'dropload-up',
                domRefresh : '<div class="dropload-refresh">↓下拉刷新</div>',
                domUpdate : '<div class="dropload-update">↑释放更新</div>',
                domLoad : '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
            },
            domDown:{
                domClass : 'dropload-down',
                domRefresh : '<div class="dropload-refresh">↑上拉加载更多</div>',
                domLoad : '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                domNoData : '<div class="dropload-noData">已经到底了，别扯了</div>'
            },
            loadDownFn:function(me){
                moreList();
                function moreList(){
                    index++;
                    message++;
                    $.ajax({
                        url:host+'/CMSAPI/MainInList?index='+index+'&size='+size+'&categorys='+fid+'&menuID=7&cid='+cid,
                        type:'GET',
                        dataType:'JSON',
                        success:function(data){
                            if(data.state){
                                var msgNum =Math.ceil(data.data.sum/size);
                                if (message <=msgNum){
                                    var html3='';
                                    var list3=data.data.list;
                                    $.each(list3,function(i,obj){
                                        var title=obj.title||'';
                                        var dataId=obj.id;
                                        var imgUrl=obj.images.length>=1?obj.images[0].URL:'';
                                        // var pubDate=obj.publishDate||'';
                                        var summary=obj.summary||'';
                                        html3+='<li class="knowledgeList" data-id="'+dataId+'"><a href="detail.html?did='+dataId+'">';
                                        html3+=    '<div class="knowledge">';
                                        html3+=    '<div class="knowledgeTitle">'+title+'</div>';
                                        // html3+=    '<div class="knowledgeTime">【'+pubDate+'】</div>'
                                        html3+=    '<div class="knowledgeSummary">'+summary+'</div>'
                                        html3+=    '</div>';
                                        html3+=    '<div class="knowledgeImg" style="background-image: url('+imgUrl+')"></div>';
                                        html3+='</a></li>'
                                    });
                                    $('.knowledgeBox').append(html3);
                                }else {
                                    me.lock();
                                    me.noData();
                                }
                            }else {
                                alert('网络繁忙，请稍后重试！')
                            }
                            me.resetload();
                        }
                    })
                }
                /********tab点击跳转******/
                $('.tabBox').unbind('click').on('click','li',function(){
                    me.unlock();
                    me.noData(false);
                    index=0;
                    message=0;
                    $('.knowledgeBox').html('');
                    $(this).addClass('tabActive').siblings('.tabActive').removeClass('tabActive');
                    var dataId=$(this).attr('data-id');
                    fid=dataId;
                    moreList(dataId);
                });
            }
            // loadUpFn:function(me){
            //     location.reload();
            //     me.resetload();
            // }
        });
    }
    /********列表点击跳转******/
    // $('.knowledgeBox').on('click','li',function(){
    //     var dataId=$(this).attr('data-id');
    //     window.location.href='detail.html?did='+dataId;
    // })
});